/*
 * Copyright (C), 2014-2015, 杭州小卡科技有限公司
 * Created by ciwei@xiaokakeji.com on 2016/06/30.
 */
package com.micromall.utils;

import java.math.BigDecimal;

/**
 * @author ciwei@xiaokakeji.com
 * @date 2016/06/30.
 */
public class Scale {

	public static BigDecimal scale(int scale, BigDecimal v) {

		return scale(scale, v.toPlainString());
	}

	public static BigDecimal scale(int scale, float v) {

		return scale(scale, String.valueOf(v));
	}

	public static BigDecimal scale(int scale, double v) {

		return scale(scale, String.valueOf(v));
	}

	public static BigDecimal scale(int scale, String v) {
		int index = v.indexOf(".");
		if (index != -1 && v.substring(index).length() >= (scale + 1)) {
			v = v.substring(0, index) + v.substring(index, index + (scale + 1));
		}
		return new BigDecimal(v);
	}

	public static void main(String[] args) {
		System.out.println(Scale.scale(2, 0.01f));
		System.out.println(Scale.scale(2, 0.011f));
		System.out.println(Scale.scale(2, 0.019f));
		System.out.println(Scale.scale(2, "0.019"));
	}
}
